<?php

namespace app\common\service;

use app\common\model\AdminPower;
use app\common\model\RuinuoButton;
use app\common\model\AdminMenu;
use think\facade\Request;

class Power
{
    /*按钮显示权限*/
    public function Button($mode_id = 0, $position = 1)
    {
        $RuinuoButton = new RuinuoButton();
        $Button = $RuinuoButton->where("mode_id='" . $mode_id . "' and status=1 and position=" . $position . " and node in(1,3)")->orderRaw('sort asc')->select()->toArray();
        $ShowButton = [];
        foreach ($Button as $k => $v) {
            if (GetToken('is_super') == 1) {
                $power = 1;
            } else {
                $power = $this->GetPower(['id' => $v['id'], 'cat' => 'button']);
            }

            if ($power == 1) {
                $query = '';
                if ($v['query']) {
                    $query = '?' . $v['query'];
                }
                $item = [
                    'title' => $v['title'],
                    'icon' => $v['icon'],
                    'btn_class' => $v['btn_class'],
                    'is_drop' => $v['is_drop'],
                    'type' => $v['type'],
                    'url' => '/' . $v['app_num'] . '/' . $v['mode_num'] . '/' . $v['action'] . '/' . $query,
                    'where' => $v['where'],
                ];
                if ($v['type'] != 'ajax') {
                    $item['width'] = $v['width'];
                    $item['height'] = $v['height'];
                }
                $ShowButton[] = $item;
            }
        }
        return $ShowButton;
    }

    /*节点权限*/
    public function Node()
    {
        if (GetToken('is_super') == 1) {
            return 1;
        }
        $RuinuoButton = new RuinuoButton();
        $AdminMenu = new AdminMenu();
        $app = strtolower(app('http')->getName());
        $controller = strtolower(Request::controller());
        $action = strtolower(Request::action());
        $url = strtolower(Request::url());
        if ($action == 'upfield') {
            $action = "edit";
        }
        if ($action == 'templateexcel') {
            $action = "inexcel";
        }
        if ($action == 'detail') {
            $action = input("node") ?: 'detail';
        }
        $powers = [];
        $where = "LOWER(app)=LOWER('" . $app . "') and LOWER(controller)=LOWER('" . $controller . "') and LOWER(action)=LOWER('" . $action . "')";
        $menu = $AdminMenu->where($where)->field("id")->select()->toArray();
        foreach ($menu as $k => $v) {
            $powers[] = [
                'id' => $v['id'],
                'cat' => 'menu'
            ];
        }
        $where = "LOWER(app_num)=LOWER('" . $app . "') and LOWER(mode_num)=LOWER('" . $controller . "') and LOWER(action)=LOWER('" . $action . "') and node in(1,2)";
        $button = $RuinuoButton->where($where)->field("id,status")->select()->toArray();
        foreach ($button as $k => $v) {
            $powers[] = [
                'id' => $v['id'],
                'cat' => 'button'
            ];
            $status = !empty($v['status']) ? $v['status'] : 1;
            if ($status == 2) {
                return 2;
            }
        }
        if (count($powers) == 0) {
            return 1;
        }
        $power = 2;
        foreach ($powers as $k => $v) {
            if ($power == 2) {
                $power = $this->GetPower($v);
            }
        }
        return $power;
    }

    public function GetPower($item)
    {
        $value = 2;
        $AdminPower = new AdminPower();
        $powerWhere = "cat='" . $item['cat'] . "' and (cat_ids='" . $item['id'] . "' or FIND_IN_SET(" . $item['id'] . ",cat_ids))";
        $powerWhere .= " and ((mark='user' and mark_id='" . GetToken('id') . "')";
        $powerWhere .= " or (mark='role' and mark_id='" . GetToken('role_ids') . "')";
        $powerWhere .= " or (mark='department' and mark_id='" . GetToken('department_ids') . "'))";
        $userPower = $AdminPower->where($powerWhere)->count();
        if ($userPower) {
            $value = 1;
        }
        return $value;
    }


    public function FormField($rule = [], $detail = [])
    {

        $action = strtolower(Request::action());
        if ($action == "detail") {
            if (input("node")) {
                $action = input("node") . '_detail';
            }
        }
        $data = [
            'power' => [],
            'detail' => $detail,
        ];


        $newRole = [];
        foreach ($rule as $k => $v) {
            $newRole[$v['field']] = $v;
        }
        $RuleNum = $action;
        if ($action == 'add_detail') {
            $RuleNum = "add";
        }
        if ($action == 'edit_detail') {
            $RuleNum = "edit";
        }

        switch ($action) {
            case "detail":
            case "add_detail":
            case "edit_detail":
                foreach ($detail as $k => $v) {
                    $field = $k;
                    $RuleData = !empty($newRole[$field]) ? $newRole[$field] : [];
                    $power = $this->PowerFormField($RuleNum, $RuleData);
                    if ($power == 1) {
                        if ($action == "detail") {
                            $data['detail'][$field] = '无权查看';
                        } else {
                            $data['power'][$field] = 1;
                        }
                    }
                    if ($action == 'edit_detail') {
                        $power = $this->PowerFormField('detail', $RuleData);
                        if ($power == 1) {
                            $data['detail'][$field] = '无权查看';
                        }
                    }
                }
                break;
            case "add":
            case "edit":
                foreach ($detail as $k => $v) {
                    $table = $k;
                    foreach ($v as $k2 => $v2) {
                        $field = $k2;
                        if ($table != "a") {
                            $field = $table . '__' . $field;
                        }
                        $RuleData = !empty($newRole[$field]) ? $newRole[$field] : [];
                        $power = $this->PowerFormField($RuleNum, $RuleData);
                        if ($power == 1) {
                            unset($detail[$table][$k2]);
                        }
                    }
                }
                $data = $detail;
                break;
        }
        return $data;
    }

    public function PowerFormField($num, $data)
    {
        if (GetToken('is_super') == 1) {
            return 2;
        }
        $status = !empty($data[$num]) ? $data[$num] : 2;
        $user_ids = !empty($data['user_ids']) ? $data['user_ids'] : '';
        $role_ids = !empty($data['role_ids']) ? $data['role_ids'] : '';
        $department_ids = !empty($data['department_ids']) ? $data['department_ids'] : '';
        if ($status == 1) {

        }
        return $status;
    }


}